import echarts from 'echarts'
import axios from 'axios'

const install = function (Vue) {
    Object.defineProperties(Vue.prototype, {
        $chart: {
            get: function () {
                return {
                    line1: function (id,chartType) {
                        let chart = echarts.init(document.getElementById(id));
                        chart.clear();
                        chart.setOption({
                            xAxis: {
                                type: 'category',
                                data: []
                            },
                            yAxis: {
                                min:chartType == 'trade'?15000:-20,
                                type: 'value'
                            },
                            tooltip : {
                                trigger: 'axis',
                                axisPointer: {
                                    type: 'cross',
                                    label: {
                                        backgroundColor: '#6a7985'
                                    }
                                }
                            },
                            dataZoom:[
                                {
                                    type: 'slider',
                                    xAxisIndex: 0,
                                    start: 10,
                                    end: 60
                                },
                                {
                                    type: 'inside',
                                    xAxisIndex: 0,
                                    start: 10,
                                    end: 60
                                },
                            ],
                            series: [{
                                data: [],
                                type: 'line',
                                smooth: true
                            }]
                        });
                        chart.showLoading();

                        let sTime = (chartType === 'trade' ? this.$cookies.get('t_sTime'):this.$cookies.get('w_sTime'));
                        let eTime = (chartType === 'trade' ? this.$cookies.get('t_eTime'):this.$cookies.get('w_eTime'));
                        let column = (chartType === 'trade' ? this.$cookies.get('t_column'):this.$cookies.get('w_column'));
                        let other = (chartType === 'trade' ? this.$cookies.get('t_subject'):this.$cookies.get('w_city'));

                        axios
                            .get('http://localhost:8080/'+chartType+'/get',
                                {
                                    params:{
                                        sTime: sTime,
                                        eTime: eTime,
                                        column: column,
                                        subject: other,
                                    }
                                })
                            .then(function (response) {
                                console.log(response.data.content.category);
                                chart.setOption({
                                    xAxis: {
                                        data: response.data.content.category
                                    },
                                    yAxis: {
                                        min: response.data.content.min,
                                        type: 'value'
                                    },
                                    series: [{
                                        name: response.data.content.column,
                                        data: response.data.content.data,
                                        label:{
                                            normal:{
                                                show:true,
                                                position:'top'
                                            }
                                        }
                                    }]
                                });
                                chart.hideLoading();
                            })
                    }
                }
            }
        }
    })
};

export default {
    install
}